using BuildingBlocks.Application.Authentication;
using BuildingBlocks.Application.Caching;
using BuildingBlocks.Application.Clock;
using BuildingBlocks.Infrastructure.Authentication;
using BuildingBlocks.Infrastructure.Caching;
using BuildingBlocks.Infrastructure.Clock;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace BuildingBlocks.Infrastructure;

public static class DependencyInjection
{
    extension(IServiceCollection services)
    {
        public IServiceCollection AddInfrastructure(IConfiguration configuration)
        {
            services.AddHttpContextAccessor();
            services.AddScoped<ICurrentUser, UserContext>();
            services.AddDateTimeProvider();
            services.AddCaching(configuration);
            return services;
        }

        private IServiceCollection AddDateTimeProvider()
        {
            services.TryAddSingleton<IDateTimeProvider, DateTimeProvider>();
            return services;
        }

        private IServiceCollection AddCaching(IConfiguration configuration)
        {
            var redisConnectionString = configuration.GetConnectionString("Redis");
            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = redisConnectionString;
            });
            services.TryAddSingleton<ICacheService, CacheService>();
            return services;
        }
    }
}
